package com.wuliu.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.SecurityUtils;
import com.wuliu.common.utils.StringUtils;
import com.wuliu.common.utils.ThreadLocalContext;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.BasEmployeeMapper;
import com.wuliu.system.domain.BasEmployee;
import com.wuliu.system.service.IBasEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 员工资料Service业务层处理
 *
 * @author realue
 * @date 2023-10-24
 */
@Service
public class BasEmployeeServiceImpl extends ServiceImpl<BasEmployeeMapper, BasEmployee> implements IBasEmployeeService {
    @Resource
    private BasEmployeeMapper basEmployeeMapper;

    /**
     * 查询员工资料
     *
     * @param id 员工资料主键
     * @return 员工资料
     */
    @Override
    public BasEmployee selectBasEmployeeById(Long id) {
        return basEmployeeMapper.selectBasEmployeeById(id);
    }

    /**
     * 查询员工资料列表
     *
     * @param basEmployee 员工资料
     * @return 员工资料
     */
    @Override
    public List<BasEmployee> selectBasEmployeeList(BasEmployee basEmployee) {
        return basEmployeeMapper.selectBasEmployeeList(basEmployee);
    }

    /**
     * 新增员工资料
     *
     * @param basEmployee 员工资料
     * @return 结果
     */
    @Override
    public int insertBasEmployee(BasEmployee basEmployee) {
        long count = basEmployeeMapper.selectCount(new LambdaQueryWrapper<BasEmployee>()
                .eq(BasEmployee::getSysAccount, basEmployee.getSysAccount())
                .eq(BasEmployee::getEmpNo, basEmployee.getEmpNo()));
        basEmployee.setCreateTime(DateUtils.getNowDate());
        if (count > 0) {
            throw new ServiceException("员工编号已存在");
        }
        if (StringUtils.isEmpty(basEmployee.getEntryDate())) {
            basEmployee.setEntryDate(DateUtils.dateTime());
        }
        return basEmployeeMapper.insertBasEmployee(basEmployee);
    }

    /**
     * 修改员工资料
     *
     * @param basEmployee 员工资料
     * @return 结果
     */
    @Override
    public int updateBasEmployee(BasEmployee basEmployee) {
        basEmployee.setUpdateTime(DateUtils.getNowDate());
        return basEmployeeMapper.updateBasEmployee(basEmployee);
    }

    /**
     * 批量删除员工资料
     *
     * @param ids 需要删除的员工资料主键
     * @return 结果
     */
    @Override
    public int deleteBasEmployeeByIds(Long[] ids) {
        return basEmployeeMapper.deleteBasEmployeeByIds(ids);
    }

    /**
     * 删除员工资料信息
     *
     * @param id 员工资料主键
     * @return 结果
     */
    @Override
    public int deleteBasEmployeeById(Long id) {
        return basEmployeeMapper.deleteBasEmployeeById(id);
    }

    /**
     * 查询司机列表 app端
     *
     * @return
     */
    @Override
    public List<BasEmployee> getDrivers() {
        SysAppletUser user = SecurityUtils.getAppletUser();
        return basEmployeeMapper.selectList(new LambdaQueryWrapper<BasEmployee>()
                .eq(BasEmployee::getSysAccount, user.getSysAccount())
                .eq(BasEmployee::getEmpType, 2));
    }
}
